package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.entity.Dept;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zyx
 * @created: 2025-10-07-19:49
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService CourseService;
    @GetMapping
    public Result page(@RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "subject", required = false) Integer subject,
                       @RequestParam(name = "target", required = false) Integer target,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("分页查询部门,参数：课程名称={},课程学科={},适用人群{}，当前页码={},每页大小={}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult = CourseService.page(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程id:{}", id);
        Course Course = CourseService.getById(id);
        return Result.success(Course);
    }

    @PutMapping
    public Result update(@RequestBody Course Course) {
        log.info("修改部门:{}", Course);
        CourseService.update(Course);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course Course) {
        log.info("添加课程:{}", Course);
        CourseService.add(Course);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程id:{}", id);
        CourseService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有课程");
        List<Course> list = CourseService.list();
        return Result.success(list);
    }
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        log.info("根据学科查询课程,参数：课程学科={}", subject);
        List<Course> list = CourseService.listBySubject(subject);
        return Result.success(list);
    }
}
